﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Utility;
using Microsoft.AspNetCore.Http;
using JFnetcore.Models;
using static Utility.EnumUtil;
using Services.IService;
using Models;
using System.Text;
using System.Linq.Expressions;

namespace JFnetcore.Controllers
{
    public class HomeController : BaseController
    {

        private IUsersService _userService;
        private IRoleService _roleService;
        private ILogService _logService;
        private IVmenuService _vmenuService;
        public HomeController(IUsersService userService, IRoleService roleService, ILogService logService, IVmenuService vmenuService)
        {
            _userService = userService;
            _roleService = roleService;
            _logService = logService;
            _vmenuService = vmenuService;
        }


        #region 主页
        /// <summary>
        /// 打开主页视图
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            SessionModel userSession = getUserSession();
            if (userSession != null)
            {
                ViewBag.User_name = userSession.User.User_name;
                ViewBag.Login_name = userSession.User.Login_name;
                //ViewBag.Rootmenu = _roleService.GetRoleRootMenu(userSession.User.Role_id);
                ViewBag.Rootmenu = GetRoleRootMenuHtml(userSession.User.Role_id);


                return View();
            }
            else
                return Redirect("/Login/Index/out");
        }

        public string GetRoleRootMenuHtml(int roleId)
        {

            StringBuilder menuhtml = new StringBuilder();
            Expression<Func<VmenuModel, bool>> Rolemenuwhere = LinqUtil.True<VmenuModel>();
            Rolemenuwhere = Rolemenuwhere.AndAlso(i => i.Role_Id == roleId && i.Disable == 1 && i.Parent_Id==0 && i.MenuDisable==1);
            List<VmenuModel> list = _vmenuService.GetList(Rolemenuwhere).OrderBy(i=>i.Menu_rank).ToList();
            int x = 0;
            foreach (VmenuModel menu in list)
            {
                if (x == 0)
                    menuhtml.Append("<li class=\"active\">");
                else
                    menuhtml.Append("<li>");

                menuhtml.Append("    <a href=\"/home/homeMenu/" + menu.Id.ToString() + "\" data-toggle=\"sidenav\" data-id-key=\"targetid\">" + menu.Menu_name + "</a>");
                menuhtml.Append("</li>");
                x++;
            }
            return menuhtml.ToString();
        }

        /// <summary>
        /// 加载菜单
        /// </summary>
        /// <param name="id">根目录菜单id</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult homeMenu(string id)
        {
            SessionModel userSession = getUserSession();
            if (userSession != null)
            {
                StringBuilder menuhtml = new StringBuilder();
                List<VmenuModel> firstlist = _vmenuService.GetListByRoleidAndMenurootid(getUserSession().User.Role_id, Int32.Parse(id), 1).Where(i => i.MenuDisable == 1).ToList();
                menuhtml.Append("[");
                int y = 0;
                foreach (VmenuModel menu1 in firstlist)
                {

                    menuhtml.Append("{");
                    menuhtml.Append("\"name\": \"" + menu1.Menu_name + "\",");
                    menuhtml.Append("\"children\": [");

                    List<VmenuModel> secondlist = _vmenuService.GetListByRoleidAndMenurootid(getUserSession().User.Role_id, menu1.Id, 1);//根据二级目录ID获取三级目录[可用]菜单,并排序
                    int x = 0;
                    foreach (VmenuModel menu2 in secondlist)
                    {

                        menuhtml.Append("{");
                        menuhtml.Append("\"id\": \"" + menu2.Menu_idname + "\",");
                        menuhtml.Append("\"name\": \"" + menu2.Menu_name + "\",");
                        menuhtml.Append("\"target\": \"" + menu2.Menu_target + "\",");
                        menuhtml.Append("\"url\": \"" + menu2.Menu_url + "\"");
                        menuhtml.Append("}");
                        menuhtml.Append(",");
                        x++;

                    }
                    if (x > 0)
                        menuhtml.Remove(menuhtml.ToString().LastIndexOf(','), 1);
                    menuhtml.Append("]");
                    menuhtml.Append("}");
                    menuhtml.Append(",");
                    y++;


                }
                if (y > 0)
                    menuhtml.Remove(menuhtml.ToString().LastIndexOf(','), 1);
                menuhtml.Append("]");
                return Content(menuhtml.ToString());
            }
            else
            {
                return Content(ReturnJsonHelp.BJUIajaxJson(301, "Session超时！", "", "", "", false, "", ""));
            }
        }

        /// <summary>
        ///退出登录 
        /// </summary>
        /// <returns></returns>
        [HttpGet("Layout")]
        public IActionResult Layout()
        {
            SessionModel sm = getUserSession();
            _logService.Add(DateTime.Now, sm.IP, sm.User.Id.ToString(), (int)OperationEnum.Logout, "");
            HttpContext.Session.SetString("JinFanUser", "");
            return Redirect("/Login/Index/out");
        }

        #endregion


        public IActionResult empty()
        {
            return View();
        }
    }
}
